﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GameLogic;

namespace FiveInARow.Components
{
	public class Cell : Control
	{
		#region .ctors

		static Cell()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(Cell), new FrameworkPropertyMetadata(typeof(Cell)));
		}

		#endregion .ctors

		#region Properties

		public CellValue Value
		{
			get { return (CellValue)GetValue(ValueProperty); }
			set { SetValue(ValueProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ValueProperty =
			DependencyProperty.Register("Value", typeof(CellValue), typeof(Cell), new UIPropertyMetadata(CellValue.Unset));

		public ICommand SetValueCommand
		{
			get { return (ICommand)GetValue(SetValueCommandProperty); }
			set { SetValue(SetValueCommandProperty, value); }
		}

		// Using a DependencyProperty as the backing store for SetValueCommand.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty SetValueCommandProperty =
			DependencyProperty.Register("SetValueCommand", typeof(ICommand), typeof(Cell), new UIPropertyMetadata(null));

		#endregion Properties

		#region Methods

		public void Set()
		{
			if (SetValueCommand != null)
			{
				if (SetValueCommand.CanExecute(DataContext))
				{
					SetValueCommand.Execute(DataContext);

					if (BindingOperations.GetBindingExpression(this, ValueProperty) != null)
					{
						BindingOperations.GetBindingExpression(this, ValueProperty).UpdateTarget();
					}
				}
			}
		}

		#endregion Methods
	}
}
